using System;
using System.Reflection;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;


namespace stsdev {

  class WebServiceProvider : ISolutionProvider {

    public string Title {
      get { return "Web Service Solution (C# assembly)"; }
    }

    public void InitializeSolution() {
      SolutionBuilder.AssemblyDeployment = true;
      SolutionBuilder.SafeControlSettings = false;
      SolutionBuilder.DeploymentTarget = AssemblyDeploymentTarget.GlobalAssemblyCache;
      SolutionBuilder.CasPermissions = false;
    }

    public void AddSolutionItems() {
      SolutionBuilder.ProjectAssemblyReferences.Add("System.Web.Services");

      // create \ISAPI Folder
      if (!Directory.Exists(Globals.IsapiFolder)) {
        Directory.CreateDirectory(Globals.IsapiFolder);
      }

      string SolutionSpecificIsapiFolder = Globals.IsapiFolder + @"\" + SolutionBuilder.SolutionName;
      if (!Directory.Exists(SolutionSpecificIsapiFolder)) {
        Directory.CreateDirectory(SolutionSpecificIsapiFolder);
      }

      string AsmxFilePath = SolutionSpecificIsapiFolder + @"\" + SolutionBuilder.SolutionName + "Service.asmx";
      if (!File.Exists(AsmxFilePath)) {
        // create config.aspx
        FileStream service_asmx = File.Create(AsmxFilePath);
        StreamWriter service_asmx_writer = new StreamWriter(service_asmx);
        string service_asmx_content = Properties.Resources.Service_asmx.Replace("@SolutionName", SolutionBuilder.SolutionName);
        service_asmx_writer.Write(service_asmx_content.Replace("@FullAssemblyName", SolutionBuilder.FullAssemblyName));
        service_asmx_writer.Dispose();
        service_asmx.Dispose();
        ProjectFileBuilder.AddSourceFile(AsmxFilePath, SourceFileTypeEnum.None);

        // create code-behind config
        string CodeBehindFilePath = SolutionBuilder.SolutionName + "Service.cs";
        FileStream service_cs = File.Create(CodeBehindFilePath);
        StreamWriter service_cs_writer = new StreamWriter(service_cs);
        service_cs_writer.Write(Properties.Resources.Service_cs.Replace("@SolutionName", SolutionBuilder.SolutionName));
        service_cs_writer.Dispose();
        service_cs.Dispose();
        ProjectFileBuilder.AddSourceFile(CodeBehindFilePath);
      }

      string WebConfigFilePath = SolutionSpecificIsapiFolder + @"\web.config";
      if (!File.Exists(WebConfigFilePath)) {
        // create config.aspx
        FileStream web_config = File.Create(WebConfigFilePath);
        StreamWriter web_config_writer = new StreamWriter(web_config);
        string web_config_content = Properties.Resources.service_web_config;
        web_config_writer.Write(web_config_content);
        web_config_writer.Dispose();
        web_config.Dispose();
        ProjectFileBuilder.AddSourceFile(WebConfigFilePath, SourceFileTypeEnum.None);
      }
    }
  
  }
}
